package com.example.onlinejudge.config;

import com.example.onlinejudge.component.CodeInValidException;
import com.example.onlinejudge.component.ProblemNotFoundException;
import com.example.onlinejudge.controller.CompileController;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.io.IOException;

@ControllerAdvice(assignableTypes = CompileController.class)
public class CompileExceptionAdvice {

    static class CompileResponse {
        //0代表没有错误
        public int error;
        public String reason;
        public String stdout;
    }
    CompileResponse compileResponse=new CompileResponse();

    @ExceptionHandler(IOException.class)
    public void ioExceptionAdvice(IOException e){
        e.printStackTrace();
    }
    @ExceptionHandler(InterruptedException.class)
    public void interruptedExceptionAdvice(InterruptedException e){
        e.printStackTrace();
    }
    @ExceptionHandler(ProblemNotFoundException.class)
    public CompileResponse problemNotFoundExceptionAdvice(ProblemNotFoundException e){
        compileResponse.error = 3;
        compileResponse.reason = "没有找到指定的题目! ";
        return compileResponse;
    }
    @ExceptionHandler(CodeInValidException.class)
    public CompileResponse codeInValidExceptionAdvice(CodeInValidException e){
        compileResponse.error = 3;
        compileResponse.reason = "提交的代码不符合要求!";
        return compileResponse;
    }
}
